package org.wenhuayuan.project.main.biz

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import com.alibaba.android.arouter.facade.annotation.Route
import kotlinx.android.synthetic.main.activity_register.*
import org.wenhuayuan.library.resful.HiCallback
import org.wenhuayuan.library.resful.HiResponse
import org.wenhuayuan.project.common.uicomp.HiBaseActivity
import org.wenhuayuan.project.main.R
import org.wenhuayuan.project.main.http.ApiFactory
import org.wenhuayuan.project.main.http.api.AccountApi
@Route(path = "/account/registration")
class RegisterActivity : HiBaseActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_register)

        action_back.setOnClickListener {
            onBackPressed()
        }
        action_submit.setOnClickListener { submit() }
    }

    private fun submit() {
        val orderId = input_item_orderId.getEditText().text.toString()
        val moocId = input_item_moocId.getEditText().text.toString()

        val userName = input_item_username.getEditText().text.toString()
        val pwd = input_item_pwd.getEditText().text.toString()
        val pwdSec = input_item_pwd_check.getEditText().text.toString()


//        if (TextUtils.isEmpty(orderId)
////        or TextUtils.isEmpty(moocId)
////        or TextUtils.isEmpty(userName)
////        or TextUtils.isEmpty(pwd)
////        or !TextUtils.equals(pwd, pwdSec)) {
////            Toast.makeText(this, "输入内容不能为空", Toast.LENGTH_SHORT).show()
////            Log.e("register", userName + pwd+pwdSec + moocId + orderId)
////            return
////        }
        ApiFactory.create(AccountApi::class.java)
            .register(userName, password = pwd, imoocId = moocId, orderId = orderId)
            .enqueue(object: HiCallback<String>{
                override fun onSuccess(response: HiResponse<String>) {

                    if (response.code == HiResponse.SUCCESS) {
                        Log.e("注册请求成功", response.data.toString())
                        val intent = Intent()
                        intent.putExtra("username", userName)
                        setResult(Activity.RESULT_OK, intent)
                        finish()
                    } else {
                        Toast.makeText(this@RegisterActivity, "onSuccess注册失败", Toast.LENGTH_SHORT).show()
                    }

                    intent.putExtra("username", userName)
                    setResult(Activity.RESULT_OK, intent)
                    finish()

                }

                override fun onFailed(throwable: Throwable) {
                    Toast.makeText(this@RegisterActivity, "onFailed注册失败", Toast.LENGTH_SHORT).show()
                }
            })

    }

}
